﻿using System.Linq;
using Missbot.Common;

namespace Missbot.AIModule.OnlineStore
{
    public static class ItemHelper
    {
        private static HonorSvc HonorSvc => AutofacSvc.Resolve<HonorSvc>();
        
        public static string ItemIncome(this ItemCollectionRecord record, string itemName, int count = 1)
        {
            var isNew = false;
            var honorName = HonorSvc.FindHonorName(itemName);
            var honor = HonorSvc.FindHonor(honorName);
            if (!record.HonorCollections.ContainsKey(honorName))
            {
                var newColl = new HonorItemCollection { Name = honorName, Type = honor.IsLimit ? HonorType.Limit : HonorType.Normal };
                record.HonorCollections.Add(honorName, newColl);
                isNew = true;
            }

            var collection = record.HonorCollections[honorName];
            if (!collection.Items.ContainsKey(itemName))
            {
                collection.Items.Add(itemName, 0);
                isNew = true;
            }

            collection.Items[itemName] += count;
            record.Update();

            if (!isNew || collection.Items.Count < honor.Items.Count)
            {
                return $"成就 【{honor.FullName()}】 完成度：{collection.Items.Count}/{honor.Items.Count}";
            }

            return $"恭喜你解锁了成就 【{honor.FullName()}】! (集齐物品：{string.Join("，", honor.Items.Select(p => p.Name))})";
        }
    }
}